package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type ConfigEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	ServiceName string // 服务名称
	ServiceType string // 服务类型
	ConfigName  string // 配置名称
	Remark      string // 描述

	ConfigValue map[string]interface{} // 配置参数
	ConfigParam map[string]interface{} // 扩展参数
}

type configPo struct {
	Id          int64                  `json:"id" db:"id"`                    // 对象id
	CreateTime  int64                  `json:"createTime" db:"create_time"`   // 创建时间
	UpdateTime  int64                  `json:"updateTime" db:"update_time"`   // 更新时间
	ServiceName string                 `json:"serviceName" db:"service_name"` // 服务名称
	ServiceType string                 `json:"serviceType" db:"service_type"` // 服务类型
	ConfigName  string                 `json:"configName" db:"config_name"`   // 配置名称
	Remark      string                 `json:"remark" db:"remark"`            // 描述
	ConfigValue map[string]interface{} `json:"configValue" db:"config_value"` // 配置参数
	ConfigParam map[string]interface{} `json:"configParam" db:"config_param"` // 扩展参数
}

func (e *ConfigEntity) Type() string {
	return "ConfigEntity"
}

func (e *ConfigEntity) TableName() string {
	return "tb_config"
}

func (e *ConfigEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *ConfigEntity) Instance() *ConfigEntity {
	return &ConfigEntity{
		BaseEntity:  new(commEntity.BaseEntity),
		ConfigValue: make(map[string]interface{}),
		ConfigParam: make(map[string]interface{}),
	}
}

func (e *ConfigEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *ConfigEntity) NewPo() interface{} {
	return &configPo{
		ConfigValue: make(map[string]interface{}),
		ConfigParam: make(map[string]interface{}),
	}
}

func (e *ConfigEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *ConfigEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.ServiceName)
	p = append(p, e.ServiceType)
	p = append(p, e.ConfigName)
	return p
}

func (e *ConfigEntity) BuildByJson(jsn *string) error {
	var po configPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *ConfigEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *ConfigEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*configPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.ConfigValue = po.ConfigValue
	ptr.ConfigParam = po.ConfigParam
	ptr.ServiceName = po.ServiceName
	ptr.ServiceType = po.ServiceType
	ptr.ConfigName = po.ConfigName
	ptr.Remark = po.Remark
}

func (e *ConfigEntity) BuildToPo() interface{} {
	ptr := e
	po := configPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.ConfigValue = ptr.ConfigValue
	po.ConfigParam = ptr.ConfigParam
	po.ServiceName = ptr.ServiceName
	po.ServiceType = ptr.ServiceType
	po.ConfigName = ptr.ConfigName
	po.Remark = ptr.Remark

	return &po
}
